package com.shushan.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.common.ui.UnScrollListView;
import com.shushan.home.bean.SatiscTimeBean;
import com.shushan.home.bean.SatiscToyBean;
import com.shushan.home.bean.UseAppBean;
import com.shushan.home.bean.UseSatistBean;
import com.shushan.login.bean.ChildBean;
import com.shushan.shushanbao.R;
import com.shushan.util.StringTools;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: TodayStatisAdapter
 * @Desciption: 今日学习统计
 * @author: yichaohua
 * @date: 2018-07-20
 */
public class TodayStatisAdapter extends BaseAdapter{

    private static final String TAG = "TodayStatisAdapter";

    private Context context;
    private UseSatistBean useSatistBean;

    public TodayStatisAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if(useSatistBean == null){
            return 0;
        }else {
            return (useSatistBean.getSatiscTimeBeanList().size()
                    + useSatistBean.getSatiscToyBeanList().size());
        }

    }

    @Override
    public Object getItem(int position) {
        if(useSatistBean == null){
            return null;
        }else {
            if(position <= (useSatistBean.getSatiscTimeBeanList().size()-1)){
                return useSatistBean.getSatiscTimeBeanList().get(position);
            }else {
                return useSatistBean.getSatiscToyBeanList().get(position);
            }
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            //填充布局
            convertView =  LayoutInflater.from(context).inflate(R.layout.today_statis_list_item, parent,false);
            holder = new ViewHolder();
            //通过ID查询控件
            holder.typeTextView = convertView.findViewById(R.id.today_statis_item_type_tv);
            holder.scoreTextView = convertView.findViewById(R.id.today_statis_item_score_tv);
            holder.usetimeTextView = convertView.findViewById(R.id.today_statis_item_use_time_tv);
            holder.createtimeTextView = convertView.findViewById(R.id.today_statis_item_create_time_tv);
            holder.reachLayout = convertView.findViewById(R.id.today_statis_item_reach_layout);
            holder.reachTextView = convertView.findViewById(R.id.today_statis_item_reach_tv);
            holder.unScrollListView = convertView.findViewById(R.id.today_statis_item_lv);
            holder.divid = convertView.findViewById(R.id.today_statis_item_divid);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(position <= (useSatistBean.getSatiscTimeBeanList().size()-1)){
            List<SatiscTimeBean> satiscTimeBeanList = useSatistBean.getSatiscTimeBeanList();
            SatiscTimeBean satiscTimeBean = satiscTimeBeanList.get(position);

            holder.typeTextView.setText(satiscTimeBean.name + "得分：");
            holder.scoreTextView.setText(satiscTimeBean.score + "分");
            holder.usetimeTextView.setText("玩耍" + StringTools.formatSecondTime(satiscTimeBean.play_time));
            holder.createtimeTextView.setVisibility(View.VISIBLE);
            holder.createtimeTextView.setText(satiscTimeBean.create_time);
            holder.reachLayout.setVisibility(View.GONE);

            if(ToolUtils.isListEmpty(satiscTimeBean.getUseAppBeanList())){
                holder.unScrollListView.setVisibility(View.GONE);
                holder.divid.setVisibility(View.GONE);
            }else {
                holder.unScrollListView.setVisibility(View.VISIBLE);
                holder.divid.setVisibility(View.VISIBLE);
                holder.unScrollListView.setAdapter(new AbsCommonAdapter<UseAppBean>(context,
                        R.layout.today_app_list_item,satiscTimeBean.getUseAppBeanList()) {

                    @Override
                    public void convert(ViewHolder helper,final UseAppBean item, int selectedItem) {
                        helper.setText(R.id.today_app_item_name_tv,item.app_name);
                        helper.setText(R.id.today_app_item_time_tv,StringTools.formatSecondTime(item.duration));
                    }
                });
            }

        }else {
            List<SatiscToyBean> satiscToyBeanList = useSatistBean.getSatiscToyBeanList();
            SatiscToyBean satiscToyBean = satiscToyBeanList.get(position);

            holder.typeTextView.setText(satiscToyBean.practice_type + "得分：");
            holder.scoreTextView.setText(satiscToyBean.score + "分");
            holder.usetimeTextView.setText(satiscToyBean.create_time);
            holder.createtimeTextView.setVisibility(View.GONE);
            holder.reachLayout.setVisibility(View.VISIBLE);
            holder.unScrollListView.setVisibility(View.GONE);
            holder.reachTextView.setText((satiscToyBean.reachType==1)?"达到玩具奖励要求":"未达到玩具奖励要求");
        }

        return convertView;
    }

    public static class ViewHolder{
        TextView typeTextView;
        TextView scoreTextView;
        TextView usetimeTextView;
        TextView createtimeTextView;
        LinearLayout reachLayout;
        TextView reachTextView;
        UnScrollListView unScrollListView;
        View divid;
    }

    public UseSatistBean getUseSatistBean() {
        return useSatistBean;
    }

    public void setUseSatistBean(UseSatistBean useSatistBean) {
        this.useSatistBean = useSatistBean;
    }
}
